package com.genexususa.eo.bing.data;

import com.genexususa.eo.bing.CampaignManagementServiceClasses.*;
import com.genexususa.eo.bing.CustomerManagementServiceClasses.Account;
import com.genexususa.eo.bing.data.requests.*;
import com.genexususa.eo.bing.data.wsSubclasses.*;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import javax.xml.bind.JAXBException;
import javax.xml.bind.PropertyException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.StringReader;
import java.util.Calendar;
import java.util.GregorianCalendar;

public class Util {
    private static long startTime = 0L;
    private static long endTime = 0L;
    private static long lastDuration = 0L;

    public Util() {
    }

    public String getPropertyExceptionMessage(PropertyException e) {
        return "Error -8: " + e.getMessage();
    }

    public String getJAXBExceptionMessage(JAXBException e) {
        return "Error -9: " + e.getMessage();
    }

    public Calendar calendarFromString(String dateString) throws GxItemException {
        // String format: YYYY-MM-DDTHH:MM:SS
        Calendar calendar = null;
        String[] pieces = dateString.split("T");
        if (pieces.length == 2) {
            String[] formerNumbers = pieces[0].split("-");
            String[] latterNumbers = pieces[1].split(":");
            if (formerNumbers.length == 3 && (latterNumbers.length == 3)) {
                int year = Integer.valueOf(formerNumbers[0]);
                int month = Integer.valueOf(formerNumbers[1]) - 1;
                int day = Integer.valueOf(formerNumbers[2]);
                int hour = Integer.valueOf(latterNumbers[0]);
                int minute = Integer.valueOf(latterNumbers[1]);
                int second = Integer.valueOf(latterNumbers[2]);
                calendar = new GregorianCalendar(year, month, day, hour, minute, second);
            } else {
                throw new GxItemException();
            }
        } else {
            throw new GxItemException();
        }
        //System.out.println(calendar.get(GregorianCalendar.DAY_OF_MONTH) + "/" + calendar.get(GregorianCalendar.MONTH) + "/" + calendar.get(GregorianCalendar.YEAR) + " " + calendar.get(GregorianCalendar.HOUR_OF_DAY) + ":" + calendar.get(GregorianCalendar.MINUTE) + ":" + calendar.get(GregorianCalendar.SECOND));
        return calendar;
    }

    public Campaign[] campaignsFromXML(String xml) throws IllegalArgumentException, GxException, ParserConfigurationException, SAXException, IOException {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new ByteArrayInputStream(xml.getBytes("UTF-8")));
        NodeList campaignsNode = doc.getElementsByTagName("Bing_CampaignCollection");
        Campaign[] campaigns = null;
        GxException gxException = null;
        if (campaignsNode.getLength() > 0) {
            NodeList campaignNode = ((Element)campaignsNode.item(0)).getElementsByTagName("Campaign");
            campaigns = new Campaign[campaignNode.getLength()];
            for (int temp = 0; temp < campaignNode.getLength(); temp++) {
                try {
                    campaigns[temp] = new GxCampaign((Element)campaignNode.item(temp));
                } catch (GxItemException e) {
                    if (gxException == null) {
                        gxException = new GxException(campaignNode.getLength());
                    }
                    gxException.addException(e, temp);
                }
            }
            if (gxException != null) {
                throw gxException;
            }
        } else {
            campaigns = new Campaign[0];
        }
        return campaigns;
    }

    public GxAdGroupsRequest[] adGroupsRequestsFromXML(String xml) throws IllegalArgumentException, GxException, ParserConfigurationException, SAXException, IOException {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new ByteArrayInputStream(xml.getBytes("UTF-8")));
        NodeList adGroupsRequestsNode = doc.getElementsByTagName("Bing_AdGroupsRequestCollection");
        GxAdGroupsRequest[] adGroupsRequests = null;
        GxException gxException = null;
        if (adGroupsRequestsNode.getLength() > 0) {
            NodeList adGroupsRequestNode = ((Element)adGroupsRequestsNode.item(0)).getElementsByTagName("Bing_AdGroupsRequest");
            adGroupsRequests = new GxAdGroupsRequest[adGroupsRequestNode.getLength()];
            for (int temp = 0; temp < adGroupsRequestNode.getLength(); temp++) {
                try {
                    adGroupsRequests[temp] = new GxAdGroupsRequest((Element)adGroupsRequestNode.item(temp));
                } catch (GxItemException e) {
                    if (gxException == null) {
                        gxException = new GxException(adGroupsRequestNode.getLength());
                    }
                    gxException.addException(e, temp);
                }
            }
            if (gxException != null) {
                throw gxException;
            }
        } else {
            adGroupsRequests = new GxAdGroupsRequest[0];
        }
        return adGroupsRequests;
    }

    public AdGroup[] adGroupsFromXML(String xml) throws IllegalArgumentException, GxException, ParserConfigurationException, SAXException, IOException {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new ByteArrayInputStream(xml.getBytes("UTF-8")));
        NodeList adGroupsNode = doc.getElementsByTagName("Bing_AdGroupCollection");
        AdGroup[] adGroups = null;
        GxException gxException = null;
        if (adGroupsNode.getLength() > 0) {
            NodeList adGroupNode = ((Element)adGroupsNode.item(0)).getElementsByTagName("AdGroup");
            adGroups = new AdGroup[adGroupNode.getLength()];
            for (int temp = 0; temp < adGroupNode.getLength(); temp++) {
                try {
                    adGroups[temp] = new GxAdGroup((Element)adGroupNode.item(temp));
                } catch (GxItemException e) {
                    if (gxException == null) {
                        gxException = new GxException(adGroupNode.getLength());
                    }
                    gxException.addException(e, temp);
                }
            }
            if (gxException != null) {
                throw gxException;
            }
        } else {
            adGroups = new AdGroup[0];
        }
        return adGroups;
    }

    public GxAdsRequest[] adsRequestsFromXML(String xml) throws IllegalArgumentException, GxException, ParserConfigurationException, SAXException, IOException {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new ByteArrayInputStream(xml.getBytes("UTF-8")));
        NodeList adsRequestsNode = doc.getElementsByTagName("Bing_AdsRequestCollection");
        GxAdsRequest[] adsRequests = null;
        GxException gxException = null;
        if (adsRequestsNode.getLength() > 0) {
            NodeList adsRequestNode = ((Element)adsRequestsNode.item(0)).getElementsByTagName("Bing_AdsRequest");
            adsRequests = new GxAdsRequest[adsRequestNode.getLength()];
            for (int temp = 0; temp < adsRequestNode.getLength(); temp++) {
                try {
                    adsRequests[temp] = new GxAdsRequest((Element)adsRequestNode.item(temp));
                } catch (GxItemException e) {
                    if (gxException == null) {
                        gxException = new GxException(adsRequestNode.getLength());
                    }
                    gxException.addException(e, temp);
                }
            }
            if (gxException != null) {
                throw gxException;
            }
        } else {
            adsRequests = new GxAdsRequest[0];
        }
        return adsRequests;
    }

    public GxKeywordsRequest[] keywordsRequestsFromXML(String xml) throws IllegalArgumentException, GxException, ParserConfigurationException, SAXException, IOException {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new ByteArrayInputStream(xml.getBytes("UTF-8")));
        NodeList keywordsRequestsNode = doc.getElementsByTagName("Bing_KeywordsRequestCollection");
        GxKeywordsRequest[] keywordsRequests = null;
        GxException gxException = null;
        if (keywordsRequestsNode.getLength() > 0) {
            NodeList keywordsRequestNode = ((Element)keywordsRequestsNode.item(0)).getElementsByTagName("Bing_KeywordsRequest");
            keywordsRequests = new GxKeywordsRequest[keywordsRequestNode.getLength()];
            for (int temp = 0; temp < keywordsRequestNode.getLength(); temp++) {
                try {
                    keywordsRequests[temp] = new GxKeywordsRequest((Element)keywordsRequestNode.item(temp));
                } catch (GxItemException e) {
                    if (gxException == null) {
                        gxException = new GxException(keywordsRequestNode.getLength());
                    }
                    gxException.addException(e, temp);
                }
            }
            if (gxException != null) {
                throw gxException;
            }
        } else {
            keywordsRequests = new GxKeywordsRequest[0];
        }
        return keywordsRequests;
    }

    public AdExtension[] adExtensionsFromXML(String xml) throws IllegalArgumentException, GxException, ParserConfigurationException, SAXException, IOException {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new ByteArrayInputStream(xml.getBytes("UTF-8")));
        NodeList adExtensionsNode = doc.getElementsByTagName("Bing_AdExtensionCollection");
        AdExtension[] adExtensions = null;
        GxException gxException = null;
        if (adExtensionsNode.getLength() > 0) {
            NodeList callAdExtensionsNode = ((Element)adExtensionsNode.item(0)).getElementsByTagName("CallAdExtensionCollection");
            NodeList locationAdExtensionsNode = ((Element)adExtensionsNode.item(0)).getElementsByTagName("LocationAdExtensionCollection");
            NodeList productAdExtensionsNode = ((Element)adExtensionsNode.item(0)).getElementsByTagName("ProductAdExtensionCollection");
            NodeList siteLinksAdExtensionsNode = ((Element)adExtensionsNode.item(0)).getElementsByTagName("SiteLinksAdExtensionCollection");
            if (callAdExtensionsNode.getLength() > 0 || locationAdExtensionsNode.getLength() > 0 || productAdExtensionsNode.getLength() > 0 || siteLinksAdExtensionsNode.getLength() > 0) {
                NodeList callAdExtensionNode = ((Element)adExtensionsNode.item(0)).getElementsByTagName("CallAdExtension");
                NodeList locationAdExtensionNode = ((Element)adExtensionsNode.item(0)).getElementsByTagName("LocationAdExtension");
                NodeList productAdExtensionNode = ((Element)adExtensionsNode.item(0)).getElementsByTagName("ProductAdExtension");
                NodeList siteLinksAdExtensionNode = ((Element)adExtensionsNode.item(0)).getElementsByTagName("SiteLinksAdExtension");
                adExtensions = new AdExtension[callAdExtensionNode.getLength() + locationAdExtensionNode.getLength() + productAdExtensionNode.getLength() + siteLinksAdExtensionNode.getLength()];
                int temp = 0;
                try {
                    for (int i=0; i<callAdExtensionNode.getLength(); i++) {
                        adExtensions[temp] = new GxCallAdExtension((Element)callAdExtensionNode.item(i));
                        temp++;
                    }
                    for (int i=0; i<locationAdExtensionNode.getLength(); i++) {
                        adExtensions[temp] = new GxLocationAdExtension((Element)locationAdExtensionNode.item(i));
                        temp++;
                    }
                    for (int i=0; i<productAdExtensionNode.getLength(); i++) {
                        adExtensions[temp] = new GxProductAdExtension((Element)productAdExtensionNode.item(i));
                        temp++;
                    }
                    for (int i=0; i<siteLinksAdExtensionNode.getLength(); i++) {
                        adExtensions[temp] = new GxSiteLinksAdExtension((Element)siteLinksAdExtensionNode.item(i));
                        temp++;
                    }
                } catch (GxItemException e) {
                    if (gxException == null) {
                        gxException = new GxException(callAdExtensionNode.getLength() + locationAdExtensionNode.getLength() + productAdExtensionNode.getLength() + siteLinksAdExtensionNode.getLength());
                    }
                    gxException.addException(e, temp);
                }
                if (gxException != null) {
                    throw gxException;
                }
            } else {
                adExtensions = new AdExtension[0];
            }
        } else {
            adExtensions = new AdExtension[0];
        }
        return adExtensions;
    }

    public Goal[] goalsFromXML(String xml) throws IllegalArgumentException, GxException, ParserConfigurationException, SAXException, IOException {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new ByteArrayInputStream(xml.getBytes("UTF-8")));
        NodeList goalsNode = doc.getElementsByTagName("Bing_GoalCollection");
        Goal[] goals = null;
        GxException gxException = null;
        if (goalsNode.getLength() > 0) {
            NodeList goalNode = ((Element)goalsNode.item(0)).getElementsByTagName("Goal");
            goals = new Goal[goalNode.getLength()];
            for (int temp = 0; temp < goalNode.getLength(); temp++) {
                try {
                    goals[temp] = new GxGoal((Element)goalNode.item(temp));
                } catch (GxItemException e) {
                    if (gxException == null) {
                        gxException = new GxException(goalNode.getLength());
                    }
                    gxException.addException(e, temp);
                }
            }
            if (gxException != null) {
                throw gxException;
            }
        } else {
            goals = new Goal[0];
        }
        return goals;
    }

    public GxSitePlacementsRequest[] sitePlacementsRequestsFromXML(String xml) throws IllegalArgumentException, GxException, ParserConfigurationException, SAXException, IOException {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new ByteArrayInputStream(xml.getBytes("UTF-8")));
        NodeList sitePlacementsRequestsNode = doc.getElementsByTagName("Bing_SitePlacementsRequestCollection");
        GxSitePlacementsRequest[] sitePlacementsRequests = null;
        GxException gxException = null;
        if (sitePlacementsRequestsNode.getLength() > 0) {
            NodeList sitePlacementsRequestNode = ((Element)sitePlacementsRequestsNode.item(0)).getElementsByTagName("Bing_SitePlacementsRequest");
            sitePlacementsRequests = new GxSitePlacementsRequest[sitePlacementsRequestNode.getLength()];
            for (int temp = 0; temp < sitePlacementsRequestNode.getLength(); temp++) {
                try {
                    sitePlacementsRequests[temp] = new GxSitePlacementsRequest((Element)sitePlacementsRequestNode.item(temp));
                } catch (GxItemException e) {
                    if (gxException == null) {
                        gxException = new GxException(sitePlacementsRequestNode.getLength());
                    }
                    gxException.addException(e, temp);
                }
            }
            if (gxException != null) {
                throw gxException;
            }
        } else {
            sitePlacementsRequests = new GxSitePlacementsRequest[0];
        }
        return sitePlacementsRequests;
    }

    public AdGroupCriterion[] adGroupCriterionsFromXML(String xml) throws IllegalArgumentException, GxException, ParserConfigurationException, SAXException, IOException {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new ByteArrayInputStream(xml.getBytes("UTF-8")));
        NodeList adGroupCriterionsNode = doc.getElementsByTagName("Bing_AdGroupCriterionCollection");
        AdGroupCriterion[] adGroupCriterions = null;
        GxException gxException = null;
        if (adGroupCriterionsNode.getLength() > 0) {
            NodeList adGroupCriterionNode = ((Element)adGroupCriterionsNode.item(0)).getElementsByTagName("AdGroupCriterion");
            adGroupCriterions = new AdGroupCriterion[adGroupCriterionNode.getLength()];
            for (int temp = 0; temp < adGroupCriterionNode.getLength(); temp++) {
                try {
                    adGroupCriterions[temp] = new GxAdGroupCriterion((Element)adGroupCriterionNode.item(temp));
                } catch (GxItemException e) {
                    if (gxException == null) {
                        gxException = new GxException(adGroupCriterionNode.getLength());
                    }
                    gxException.addException(e, temp);
                }
            }
            if (gxException != null) {
                throw gxException;
            }
        } else {
            adGroupCriterions = new AdGroupCriterion[0];
        }
        return adGroupCriterions;
    }

    public GxTargetRequest[] targetsRequestsFromXML(String xml) throws IllegalArgumentException, GxException, ParserConfigurationException, SAXException, IOException {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new ByteArrayInputStream(xml.getBytes("UTF-8")));
        NodeList targetsRequestsNode = doc.getElementsByTagName("Bing_TargetRequestCollection");
        GxTargetRequest[] targetsRequests = null;
        GxException gxException = null;
        if (targetsRequestsNode.getLength() > 0) {
            NodeList targetsRequestNode = ((Element)targetsRequestsNode.item(0)).getElementsByTagName("Bing_TargetRequest");
            targetsRequests = new GxTargetRequest[targetsRequestNode.getLength()];
            for (int temp = 0; temp < targetsRequestNode.getLength(); temp++) {
                try {
                    targetsRequests[temp] = new GxTargetRequest((Element)targetsRequestNode.item(temp));
                } catch (GxItemException e) {
                    if (gxException == null) {
                        gxException = new GxException(targetsRequestNode.getLength());
                    }
                    gxException.addException(e, temp);
                }
            }
            if (gxException != null) {
                throw gxException;
            }
        } else {
            targetsRequests = new GxTargetRequest[0];
        }
        return targetsRequests;
    }

    public Target[] targetsFromXML(String xml) throws IllegalArgumentException, GxException, ParserConfigurationException, SAXException, IOException {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new ByteArrayInputStream(xml.getBytes("UTF-8")));
        NodeList targetsNode = doc.getElementsByTagName("Bing_TargetCollection");
        Target[] targets = null;
        GxException gxException = null;
        if (targetsNode.getLength() > 0) {
            NodeList targetNode = ((Element)targetsNode.item(0)).getElementsByTagName("Target");
            targets = new Target[targetNode.getLength()];
            for (int temp = 0; temp < targetNode.getLength(); temp++) {
                try {
                    targets[temp] = new GxTarget((Element)targetNode.item(temp));
                } catch (GxItemException e) {
                    if (gxException == null) {
                        gxException = new GxException(targetNode.getLength());
                    }
                    gxException.addException(e, temp);
                }
            }
            if (gxException != null) {
                throw gxException;
            }
        } else {
            targets = new Target[0];
        }
        return targets;
    }

    public GxTargetCampaignsRequest[] targetCampaignsRequestsFromXML(String xml) throws IllegalArgumentException, GxException, ParserConfigurationException, SAXException, IOException {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new ByteArrayInputStream(xml.getBytes("UTF-8")));
        NodeList targetCampaignsRequestsNode = doc.getElementsByTagName("Bing_TargetCampaignsRequestCollection");
        GxTargetCampaignsRequest[] targetCampaignsRequests = null;
        GxException gxException = null;
        if (targetCampaignsRequestsNode.getLength() > 0) {
            NodeList targetCampaignsRequestNode = ((Element)targetCampaignsRequestsNode.item(0)).getElementsByTagName("Bing_TargetCampaignsRequest");
            targetCampaignsRequests = new GxTargetCampaignsRequest[targetCampaignsRequestNode.getLength()];
            for (int temp = 0; temp < targetCampaignsRequestNode.getLength(); temp++) {
                try {
                    targetCampaignsRequests[temp] = new GxTargetCampaignsRequest((Element)targetCampaignsRequestNode.item(temp));
                } catch (GxItemException e) {
                    if (gxException == null) {
                        gxException = new GxException(targetCampaignsRequestNode.getLength());
                    }
                    gxException.addException(e, temp);
                }
            }
            if (gxException != null) {
                throw gxException;
            }
        } else {
            targetCampaignsRequests = new GxTargetCampaignsRequest[0];
        }
        return targetCampaignsRequests;
    }

    public GxAdExtensionIdToCampaignIdAssociation [] adExtensionIdToCampaignIdAssociationsFromXML(String xml) throws IllegalArgumentException, GxException, ParserConfigurationException, SAXException, IOException {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new ByteArrayInputStream(xml.getBytes("UTF-8")));
        NodeList adExtensionIdToCampaignIdAssociationsNode = doc.getElementsByTagName("Bing_AdExtensionIdToCampaignIdAssociationCollection");
        //AdExtensionIdToCampaignIdAssociation[] adExtensionIdToCampaignIdAssociations = null;
        GxAdExtensionIdToCampaignIdAssociation [] adExtensionIdToCampaignIdAssociations = null;
        GxException gxException = null;
        if (adExtensionIdToCampaignIdAssociationsNode.getLength() > 0) {
            NodeList adExtensionIdToCampaignIdAssociationNode = ((Element)adExtensionIdToCampaignIdAssociationsNode.item(0)).getElementsByTagName("AdExtensionIdToCampaignIdAssociation");
            adExtensionIdToCampaignIdAssociations = new GxAdExtensionIdToCampaignIdAssociation[adExtensionIdToCampaignIdAssociationNode.getLength()];
            for (int temp = 0; temp < adExtensionIdToCampaignIdAssociationNode.getLength(); temp++) {
                try {
                    adExtensionIdToCampaignIdAssociations[temp] = new GxAdExtensionIdToCampaignIdAssociation((Element)adExtensionIdToCampaignIdAssociationNode.item(temp));
                } catch (GxItemException e) {
                    if (gxException == null) {
                        gxException = new GxException(adExtensionIdToCampaignIdAssociationNode.getLength());
                    }
                    gxException.addException(e, temp);
                }
            }
            if (gxException != null) {
                throw gxException;
            }
        } else {
            //adExtensionIdToCampaignIdAssociations = new AdExtensionIdToCampaignIdAssociation[0];
            adExtensionIdToCampaignIdAssociations = new GxAdExtensionIdToCampaignIdAssociation[0];
        }
        return adExtensionIdToCampaignIdAssociations;
    }

    public GxTargetIdToAdGroupIdAssociation[] targetIdToAdGroupIdAssociationsFromXML(String xml) throws IllegalArgumentException, GxException, ParserConfigurationException, SAXException, IOException {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new ByteArrayInputStream(xml.getBytes("UTF-8")));
        NodeList targetIdToAdGroupIdAssociationsNode = doc.getElementsByTagName("Bing_TargetIdToAdGroupIdAssociationCollection");
        GxTargetIdToAdGroupIdAssociation[] targetIdToAdGroupIdAssociations = null;
        GxException gxException = null;
        if (targetIdToAdGroupIdAssociationsNode.getLength() > 0) {
            NodeList targetIdToAdGroupIdAssociationNode = ((Element)targetIdToAdGroupIdAssociationsNode.item(0)).getElementsByTagName("TargetIdToAdGroupIdAssociation");
            targetIdToAdGroupIdAssociations = new GxTargetIdToAdGroupIdAssociation[targetIdToAdGroupIdAssociationNode.getLength()];
            for (int temp = 0; temp < targetIdToAdGroupIdAssociationNode.getLength(); temp++) {
                try {
                    targetIdToAdGroupIdAssociations[temp] = new GxTargetIdToAdGroupIdAssociation((Element)targetIdToAdGroupIdAssociationNode.item(temp));
                } catch (GxItemException e) {
                    if (gxException == null) {
                        gxException = new GxException(targetIdToAdGroupIdAssociationNode.getLength());
                    }
                    gxException.addException(e, temp);
                }
            }
            if (gxException != null) {
                throw gxException;
            }
        } else {
            targetIdToAdGroupIdAssociations = new GxTargetIdToAdGroupIdAssociation[0];
        }
        return targetIdToAdGroupIdAssociations;
    }

    public GxTargetIdToCampaignIdAssociation[] targetIdToCampaignIdAssociationsFromXML(String xml) throws IllegalArgumentException, GxException, ParserConfigurationException, SAXException, IOException {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new ByteArrayInputStream(xml.getBytes("UTF-8")));
        NodeList targetIdToCampaignIdAssociationsNode = doc.getElementsByTagName("Bing_TargetIdToCampaignIdAssociationCollection");
        GxTargetIdToCampaignIdAssociation[] targetIdToCampaignIdAssociations = null;
        GxException gxException = null;
        if (targetIdToCampaignIdAssociationsNode.getLength() > 0) {
            NodeList targetIdToCampaignIdAssociationNode = ((Element)targetIdToCampaignIdAssociationsNode.item(0)).getElementsByTagName("TargetIdToCampaignIdAssociation");
            targetIdToCampaignIdAssociations = new GxTargetIdToCampaignIdAssociation[targetIdToCampaignIdAssociationNode.getLength()];
            for (int temp = 0; temp < targetIdToCampaignIdAssociationNode.getLength(); temp++) {
                try {
                    targetIdToCampaignIdAssociations[temp] = new GxTargetIdToCampaignIdAssociation((Element)targetIdToCampaignIdAssociationNode.item(temp));
                } catch (GxItemException e) {
                    if (gxException == null) {
                        gxException = new GxException(targetIdToCampaignIdAssociationNode.getLength());
                    }
                    gxException.addException(e, temp);
                }
            }
            if (gxException != null) {
                throw gxException;
            }
        } else {
            targetIdToCampaignIdAssociations = new GxTargetIdToCampaignIdAssociation[0];
        }
        return targetIdToCampaignIdAssociations;
    }

    public GxAdGroupAdRotationsRequest[] adGroupAdRotationsRequestsFromXML(String xml) throws IllegalArgumentException, GxException, ParserConfigurationException, SAXException, IOException {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new ByteArrayInputStream(xml.getBytes("UTF-8")));
        NodeList adGroupAdRotationsRequestsNode = doc.getElementsByTagName("Bing_AdGroupAdRotationsRequestCollection");
        GxAdGroupAdRotationsRequest[] adGroupAdRotationsRequests = null;
        GxException gxException = null;
        if (adGroupAdRotationsRequestsNode.getLength() > 0) {
            NodeList adGroupAdRotationsRequestNode = ((Element)adGroupAdRotationsRequestsNode.item(0)).getElementsByTagName("Bing_AdGroupAdRotationsRequest");
            adGroupAdRotationsRequests = new GxAdGroupAdRotationsRequest[adGroupAdRotationsRequestNode.getLength()];
            for (int temp = 0; temp < adGroupAdRotationsRequestNode.getLength(); temp++) {
                try {
                    adGroupAdRotationsRequests[temp] = new GxAdGroupAdRotationsRequest((Element)adGroupAdRotationsRequestNode.item(temp));
                } catch (GxItemException e) {
                    if (gxException == null) {
                        gxException = new GxException(adGroupAdRotationsRequestNode.getLength());
                    }
                    gxException.addException(e, temp);
                }
            }
            if (gxException != null) {
                throw gxException;
            }
        } else {
            adGroupAdRotationsRequests = new GxAdGroupAdRotationsRequest[0];
        }
        return adGroupAdRotationsRequests;
    }

    public GxAdGroupNegativeKeywordsRequest[] adGroupNegativeKeywordsRequestsFromXML(String xml) throws IllegalArgumentException, GxException, ParserConfigurationException, SAXException, IOException {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new ByteArrayInputStream(xml.getBytes("UTF-8")));
        NodeList adGroupNegativeKeywordsRequestsNode = doc.getElementsByTagName("Bing_AdGroupNegativeKeywordsRequestCollection");
        GxAdGroupNegativeKeywordsRequest[] adGroupNegativeKeywordsRequests = null;
        GxException gxException = null;
        if (adGroupNegativeKeywordsRequestsNode.getLength() > 0) {
            NodeList adGroupNegativeKeywordsRequestNode = ((Element)adGroupNegativeKeywordsRequestsNode.item(0)).getElementsByTagName("Bing_AdGroupNegativeKeywordsRequest");
            adGroupNegativeKeywordsRequests = new GxAdGroupNegativeKeywordsRequest[adGroupNegativeKeywordsRequestNode.getLength()];
            for (int temp = 0; temp < adGroupNegativeKeywordsRequestNode.getLength(); temp++) {
                try {
                    adGroupNegativeKeywordsRequests[temp] = new GxAdGroupNegativeKeywordsRequest((Element)adGroupNegativeKeywordsRequestNode.item(temp));
                } catch (GxItemException e) {
                    if (gxException == null) {
                        gxException = new GxException(adGroupNegativeKeywordsRequestNode.getLength());
                    }
                    gxException.addException(e, temp);
                }
            }
            if (gxException != null) {
                throw gxException;
            }
        } else {
            adGroupNegativeKeywordsRequests = new GxAdGroupNegativeKeywordsRequest[0];
        }
        return adGroupNegativeKeywordsRequests;
    }

    public AdGroupNegativeKeywords[] adGroupNegativeKeywordsFromXML(String xml) throws IllegalArgumentException, GxException, ParserConfigurationException, SAXException, IOException {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new ByteArrayInputStream(xml.getBytes("UTF-8")));
        NodeList adGroupNegativeKeywordsNode = doc.getElementsByTagName("Bing_AdGroupNegativeKeywordsCollection");
        AdGroupNegativeKeywords[] adGroupNegativeKeywords = null;
        GxException gxException = null;
        if (adGroupNegativeKeywordsNode.getLength() > 0) {
            NodeList adGroupNegativeKeywordNode = ((Element)adGroupNegativeKeywordsNode.item(0)).getElementsByTagName("AdGroupNegativeKeywords");
            adGroupNegativeKeywords = new AdGroupNegativeKeywords[adGroupNegativeKeywordNode.getLength()];
            for (int temp = 0; temp < adGroupNegativeKeywordNode.getLength(); temp++) {
                try {
                    adGroupNegativeKeywords[temp] = new GxAdGroupNegativeKeywords((Element)adGroupNegativeKeywordNode.item(temp));
                } catch (GxItemException e) {
                    if (gxException == null) {
                        gxException = new GxException(adGroupNegativeKeywordNode.getLength());
                    }
                    gxException.addException(e, temp);
                }
            }
            if (gxException != null) {
                throw gxException;
            }
        } else {
            adGroupNegativeKeywords = new AdGroupNegativeKeywords[0];
        }
        return adGroupNegativeKeywords;
    }

    public CampaignNegativeKeywords[] campaignNegativeKeywordsFromXML(String xml) throws IllegalArgumentException, GxException, ParserConfigurationException, SAXException, IOException {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new ByteArrayInputStream(xml.getBytes("UTF-8")));
        NodeList campaignNegativeKeywordsNode = doc.getElementsByTagName("Bing_CampaignNegativeKeywordsCollection");
        CampaignNegativeKeywords[] campaignNegativeKeywords = null;
        GxException gxException = null;
        if (campaignNegativeKeywordsNode.getLength() > 0) {
            NodeList campaignNegativeKeywordNode = ((Element)campaignNegativeKeywordsNode.item(0)).getElementsByTagName("CampaignNegativeKeywords");
            campaignNegativeKeywords = new CampaignNegativeKeywords[campaignNegativeKeywordNode.getLength()];
            for (int temp = 0; temp < campaignNegativeKeywordNode.getLength(); temp++) {
                try {
                    campaignNegativeKeywords[temp] = new GxCampaignNegativeKeywords((Element)campaignNegativeKeywordNode.item(temp));
                } catch (GxItemException e) {
                    if (gxException == null) {
                        gxException = new GxException(campaignNegativeKeywordNode.getLength());
                    }
                    gxException.addException(e, temp);
                }
            }
            if (gxException != null) {
                throw gxException;
            }
        } else {
            campaignNegativeKeywords = new CampaignNegativeKeywords[0];
        }
        return campaignNegativeKeywords;
    }

    public GxAdGroupNegativeSitesRequest[] adGroupNegativeSitesRequestsFromXML(String xml) throws IllegalArgumentException, GxException, ParserConfigurationException, SAXException, IOException {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new ByteArrayInputStream(xml.getBytes("UTF-8")));
        NodeList adGroupNegativeSitesRequestsNode = doc.getElementsByTagName("Bing_AdGroupNegativeSitesRequestCollection");
        GxAdGroupNegativeSitesRequest[] adGroupNegativeSitesRequests = null;
        GxException gxException = null;
        if (adGroupNegativeSitesRequestsNode.getLength() > 0) {
            NodeList adGroupNegativeSitesRequestNode = ((Element)adGroupNegativeSitesRequestsNode.item(0)).getElementsByTagName("Bing_AdGroupNegativeSitesRequest");
            adGroupNegativeSitesRequests = new GxAdGroupNegativeSitesRequest[adGroupNegativeSitesRequestNode.getLength()];
            for (int temp = 0; temp < adGroupNegativeSitesRequestNode.getLength(); temp++) {
                try {
                    adGroupNegativeSitesRequests[temp] = new GxAdGroupNegativeSitesRequest((Element)adGroupNegativeSitesRequestNode.item(temp));
                } catch (GxItemException e) {
                    if (gxException == null) {
                        gxException = new GxException(adGroupNegativeSitesRequestNode.getLength());
                    }
                    gxException.addException(e, temp);
                }
            }
            if (gxException != null) {
                throw gxException;
            }
        } else {
            adGroupNegativeSitesRequests = new GxAdGroupNegativeSitesRequest[0];
        }
        return adGroupNegativeSitesRequests;
    }

    public AdGroupNegativeSites[] adGroupNegativeSitesFromXML(String xml) throws IllegalArgumentException, GxException, ParserConfigurationException, SAXException, IOException {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new ByteArrayInputStream(xml.getBytes("UTF-8")));
        NodeList adGroupNegativeSitesNode = doc.getElementsByTagName("Bing_AdGroupNegativeSitesCollection");
        AdGroupNegativeSites[] adGroupNegativeSites = null;
        GxException gxException = null;
        if (adGroupNegativeSitesNode.getLength() > 0) {
            NodeList adGroupNegativeSiteNode = ((Element)adGroupNegativeSitesNode.item(0)).getElementsByTagName("AdGroupNegativeSites");
            adGroupNegativeSites = new AdGroupNegativeSites[adGroupNegativeSiteNode.getLength()];
            for (int temp = 0; temp < adGroupNegativeSiteNode.getLength(); temp++) {
                try {
                    adGroupNegativeSites[temp] = new GxAdGroupNegativeSites((Element)adGroupNegativeSiteNode.item(temp));
                } catch (GxItemException e) {
                    if (gxException == null) {
                        gxException = new GxException(adGroupNegativeSiteNode.getLength());
                    }
                    gxException.addException(e, temp);
                }
            }
            if (gxException != null) {
                throw gxException;
            }
        } else {
            adGroupNegativeSites = new AdGroupNegativeSites[0];
        }
        return adGroupNegativeSites;
    }

    public CampaignNegativeSites[] campaignNegativeSitesFromXML(String xml) throws IllegalArgumentException, GxException, ParserConfigurationException, SAXException, IOException {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new ByteArrayInputStream(xml.getBytes("UTF-8")));
        NodeList campaignNegativeSitesNode = doc.getElementsByTagName("Bing_CampaignNegativeSitesCollection");
        CampaignNegativeSites[] campaignNegativeSites = null;
        GxException gxException = null;
        if (campaignNegativeSitesNode.getLength() > 0) {
            NodeList campaignNegativeSiteNode = ((Element)campaignNegativeSitesNode.item(0)).getElementsByTagName("CampaignNegativeSites");
            campaignNegativeSites = new CampaignNegativeSites[campaignNegativeSiteNode.getLength()];
            for (int temp = 0; temp < campaignNegativeSiteNode.getLength(); temp++) {
                try {
                    campaignNegativeSites[temp] = new GxCampaignNegativeSites((Element)campaignNegativeSiteNode.item(temp));
                } catch (GxItemException e) {
                    if (gxException == null) {
                        gxException = new GxException(campaignNegativeSiteNode.getLength());
                    }
                    gxException.addException(e, temp);
                }
            }
            if (gxException != null) {
                throw gxException;
            }
        } else {
            campaignNegativeSites = new CampaignNegativeSites[0];
        }
        return campaignNegativeSites;
    }

    public GxKeywordDestinationUrlsRequest[] keywordDestinationUrlsRequestsFromXML(String xml) throws IllegalArgumentException, GxException, ParserConfigurationException, SAXException, IOException {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new ByteArrayInputStream(xml.getBytes("UTF-8")));
        NodeList keywordDestinationUrlsRequestsNode = doc.getElementsByTagName("Bing_KeywordDestinationUrlsRequestCollection");
        GxKeywordDestinationUrlsRequest[] keywordDestinationUrlsRequests = null;
        GxException gxException = null;
        if (keywordDestinationUrlsRequestsNode.getLength() > 0) {
            NodeList keywordDestinationUrlsRequestNode = ((Element)keywordDestinationUrlsRequestsNode.item(0)).getElementsByTagName("Bing_KeywordDestinationUrlsRequest");
            keywordDestinationUrlsRequests = new GxKeywordDestinationUrlsRequest[keywordDestinationUrlsRequestNode.getLength()];
            for (int temp = 0; temp < keywordDestinationUrlsRequestNode.getLength(); temp++) {
                try {
                    keywordDestinationUrlsRequests[temp] = new GxKeywordDestinationUrlsRequest((Element)keywordDestinationUrlsRequestNode.item(temp));
                } catch (GxItemException e) {
                    if (gxException == null) {
                        gxException = new GxException(keywordDestinationUrlsRequestNode.getLength());
                    }
                    gxException.addException(e, temp);
                }
            }
            if (gxException != null) {
                throw gxException;
            }
        } else {
            keywordDestinationUrlsRequests = new GxKeywordDestinationUrlsRequest[0];
        }
        return keywordDestinationUrlsRequests;
    }

    public AccountAnalyticsType[] accountAnalyticsTypesFromXML(String xml) throws IllegalArgumentException, GxException, ParserConfigurationException, SAXException, IOException {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new ByteArrayInputStream(xml.getBytes("UTF-8")));
        NodeList accountAnalyticsTypesNode = doc.getElementsByTagName("Bing_AccountAnalyticsTypeCollection");
        AccountAnalyticsType[] accountAnalyticsTypes = null;
        GxException gxException = null;
        if (accountAnalyticsTypesNode.getLength() > 0) {
            NodeList accountAnalyticsTypeNode = ((Element)accountAnalyticsTypesNode.item(0)).getElementsByTagName("AccountAnalyticsType");
            accountAnalyticsTypes = new AccountAnalyticsType[accountAnalyticsTypeNode.getLength()];
            for (int temp = 0; temp < accountAnalyticsTypeNode.getLength(); temp++) {
                try {
                    accountAnalyticsTypes[temp] = new GxAccountAnalyticsType((Element)accountAnalyticsTypeNode.item(temp));
                } catch (GxItemException e) {
                    if (gxException == null) {
                        gxException = new GxException(accountAnalyticsTypeNode.getLength());
                    }
                    gxException.addException(e, temp);
                }
            }
            if (gxException != null) {
                throw gxException;
            }
        } else {
            accountAnalyticsTypes = new AccountAnalyticsType[0];
        }
        return accountAnalyticsTypes;
    }

    public Account[] accountsFromXML(String xml) throws IllegalArgumentException, GxException, ParserConfigurationException, SAXException, IOException {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new ByteArrayInputStream(xml.getBytes("UTF-8")));
        NodeList accountsNode = doc.getElementsByTagName("Bing_AccountCollection");
        Account[] accounts = null;
        GxException gxException = null;
        if (accountsNode.getLength() > 0) {
            NodeList accountNode = ((Element)accountsNode.item(0)).getElementsByTagName("Account");
            accounts = new Account[accountNode.getLength()];
            for (int temp = 0; temp < accountNode.getLength(); temp++) {
                try {
                    accounts[temp] = new GxAccount((Element)accountNode.item(temp));
                } catch (GxItemException e) {
                    if (gxException == null) {
                        gxException = new GxException(accountNode.getLength());
                    }
                    gxException.addException(e, temp);
                }
            }
            if (gxException != null) {
                throw gxException;
            }
        } else {
            accounts = new Account[0];
        }
        return accounts;
    }

    public static void startTrace(){
        startTime = System.nanoTime();
    }

    public static void endTrace(){
        endTime = System.nanoTime();
    }

    public static double getDuration(){
        lastDuration = endTime - startTime;
        endTime = 0L;
        startTime = 0L;

        return lastDuration / 10E09;

    }

    public Document loadXMLFromString(String xml) throws Exception{
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        factory.setNamespaceAware(true); // never forget this!
        InputSource is = new InputSource(new StringReader(xml));
        return builder.parse(is);
    }
}
